<script setup lang="ts">
import {articleFields} from "@/utils/field";
import {categoryOptions} from "@/api/article/category";
import {ArticleForm, createArticle, getArticleDetail, updateArticle} from "@/api/article";

defineOptions({
  name: "EditArticle",
  inheritAttrs: false,
});

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },

  articleId: {
    type: Number,
    default: null,
  },

});
const objFormRef = ref(ElForm);
const loading = ref(false);
const modelFields = articleFields();

const formData = reactive<ArticleForm>({
  status: 2,
  sort: 60
});


onMounted(() => {
})

onBeforeMount(() => {
  loading.value = true
  categoryOptions().then((res) => {
    modelFields['category_id'].options = res.data
  }).finally(() => {
    loading.value = false
  })
})

watch(
  () => props.articleId,
  (newVal) => {
    if (newVal) {
      getArticleDetail(newVal)
        .then(({data}) => {
          Object.assign(formData, data);
        })
    }
  }
);

const rules = reactive({
  title: [{required: true, message: "请输入名称", trigger: "blur"}],
  category_id: [{required: true, message: "请选择分类", trigger: "blur"}],
  // thumb: [{required: true, message: "请上传预览图", trigger: "blur"}],
});

/** 保存提交 */
function handleSubmit() {
  objFormRef.value.validate((valid: any) => {
    if (valid) {
      if (loading.value) {
        return;
      }
      const id = formData.id;
      loading.value = true;
      if (id) {
        updateArticle(id, formData)
          .then(() => {
            closeDialog(1)
          })
          .finally(() => (loading.value = false));
      } else {
        createArticle(formData)
          .then((data) => {
            closeDialog(1)
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

const emit = defineEmits(["close"]);

/** 关闭表单弹窗 */
function closeDialog(type?: any) {
  resetForm();
  emit("close", type || 0);
}

function uploadSucess(res: any) {
  formData[res.name] = res.value || ''
}

function resetForm() {
  objFormRef.value.resetFields();
  objFormRef.value.clearValidate();
  /*formData.id = undefined;
  formData.status = 1;
  formData.sort = 60;*/
}
</script>

<!-- 编辑表单弹窗 -->
<template>
  <el-dialog v-model="props.visible" :title="articleId?'编辑文章':'新增文章'" width="1000" @close="closeDialog">
    <el-form ref="objFormRef" :model="formData" :rules="rules" label-width="140px">
      <el-form-item
        :label="field.title"
        :prop="fk.toString()"
        v-for="(field, fk) in modelFields"
        :key="fk"
      >
        <el-radio-group v-model="formData[fk]" v-if="field.type == 'radio'">
          <el-radio
            :label="Number(ok)"
            v-for="(op, ok) in field.options"
            :key="ok"
          >{{ op }}
          </el-radio>
        </el-radio-group>

        <single-upload v-model="formData[fk]" :fkey="String(fk)" @upload-sucess="uploadSucess"
                       v-else-if="field.type == 'image'"/>
        <WangEditor v-model="formData[fk]" ref="WangEditorRef" v-else-if="field.type == 'editor'"/>

        <el-select v-model="formData[fk]" v-else-if="field.type == 'select'">
          <el-option
            :label="op.title || ''"
            v-for="op in field.options"
            :value="Number(op.id)"
            v-if="field.range_key"
          />
          <el-option
            :label="op || ''"
            v-for="(op, ok) in field.options"
            :value="Number(ok)"
            v-else
          />
        </el-select>

        <el-input
          :type="field.type || 'text'"
          v-model="formData[fk]"
          :placeholder="'请输入' + field.title"
          v-else
        />
      </el-form-item>

      <div class="clear"></div>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">保存</el-button>
        <el-button @click="closeDialog" type="default">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>

</style>
